hap_df |>
mutate(country = fct_reorder(country, gdp_per_capita))|>
plot_ly (
x = ~country, y = ~gdp_per_capita, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "GDP Per Capita",
xaxis = list(
title = "country",
tickfont = list(size = 6)
),
yaxis = list(title = "GDP Per Capita"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
hap_df |>
mutate(country = fct_reorder(country, health_life_expectancy))|>
plot_ly (
x = ~country, y = ~health_life_expectancy, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "Healthy Life Expectancy",
xaxis = list(
title = "country",
tickfont = list(size = 6)
),
yaxis = list(title = "Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
hap_df |>
mutate(
country = fct_reorder(country, freedom_to_make_life_choices))|>
plot_ly (
x = ~country, y = ~freedom_to_make_life_choices, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "Freedom to Make Life Choices",
xaxis = list(
title = "country",
tickfont = list(size = 6)
),
yaxis = list(title = "Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
hap_df |>
mutate(country = fct_reorder(country, perception_of_corruption))|>
plot_ly (
x = ~country, y = ~perception_of_corruption, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "Perception of Corruption",
xaxis = list(
title = "country",
tickfont = list(size = 6)
),
yaxis = list(title = "Score"),
autosize = F, width = 1000, height = 500
)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `country = fct_reorder(country, perception_of_corruption)`.
## Caused by warning:
## ! `fct_reorder()` removing 1 missing value.
## ℹ Use `.na_rm = TRUE` to silence this message.
## ℹ Use `.na_rm = FALSE` to preserve NAs.
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
## Warning: Ignoring 1 observations
hap_df |>
mutate(country = fct_reorder(country, generosity))|>
plot_ly (
x = ~country, y = ~generosity, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "Genersoity",
xaxis = list(
title = "country",
tickfont = list(size = 6)
),
yaxis = list(title = "Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
Iteration
factor_country_level <- function(data, variable, title) {
p = data %>%
mutate(country = fct_reorder(country, .data[[variable]])) %>%
plot_ly(
x = ~country,
y = as.formula(paste0("~", variable)),
color = ~year,
type = 'bar',
colors = 'viridis'
) %>%
layout(
title = title,
xaxis = list(title = "Country", tickfont = list(size = 6)),
yaxis = list(title = "Score"),
autosize = F, width = 1000, height = 500
)
print(p)
}
# Create a list of variables and corresponding titles
variables_and_titles <- list(
list(variable = "gdp_per_capita", title = "GDP Per Capita"),
list(variable = "social_support", title = "Social Support/Having Someone to Count On In Time of Trouble"),
list(variable = "health_life_expectancy", title = "Healthy Life Expectancy"),
list(variable = "freedom_to_make_life_choices", title = "Freedom to Make Life Choices"),
list(variable = "perception_of_corruption", title = "Perception of Corruption"),
list(variable = "generosity", title = "Generosity")
)
# Use map to create a list of plots
plots <- map(variables_and_titles, ~factor_country_level(hap_df, .x$variable, .x$title))
for (plot in plots) {
print(plot)
}
hap_df |>
group_by(year) |>
summarise(
average_gdp_per_capita = mean(gdp_per_capita, na.rm = TRUE),
average_social_support = mean(social_support, na.rm = TRUE),
average_healthy_life_expectancy = mean(health_life_expectancy, na.rm = TRUE),
average_freedom_to_make_life_choices = mean(freedom_to_make_life_choices, na.rm = TRUE),
average_perception_of_corruption = mean(perception_of_corruption, na.rm = TRUE),
average_generosity = mean(generosity, na.rm = TRUE)
)|>
pivot_longer(
average_gdp_per_capita:average_generosity,
names_to ="factor",
names_prefix = "average_",
values_to = "average_score"
)|>
plot_ly(
x = ~year, y = ~average_score, color = ~factor,
type = "scatter", mode = "lines+markers", colors = "viridis") |>
layout(
title = "World Trends of Happiness Indicators from 2015 to 2019",
xaxis = list( title = "Year"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
hap_df |>
mutate(region = fct_reorder(region, gdp_per_capita))|>
group_by(region, year)|>
summarise(
average_gdp_per_capita = mean(gdp_per_capita, na.rm = TRUE)
) |>
plot_ly (
x = ~region, y = ~average_gdp_per_capita, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "GDP Per Capita By Region",
xaxis = list(title = "Region"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
Regional Trend
regional_hap_df |>
filter(factor == "gdp_per_capita")|>
plot_ly(
x = ~year, y = ~average_score, color = ~region,
mode = 'lines+markers', type = 'scatter', colors = 'viridis',
name = ~region) |>
layout(
title = paste("Regional Trend for GDP Per Captia from 2015 to 2019"),
xaxis = list(title = "Year"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
hap_df |>
mutate(region = fct_reorder(region, health_life_expectancy))|>
group_by(region, year)|>
summarise(
average_life_expectancy = mean(health_life_expectancy, na.rm = TRUE)
) |>
plot_ly (
x = ~region, y = ~average_life_expectancy, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "Healthy Life Expectancy By Region",
xaxis = list(title = "Region"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
Regional Trend
regional_hap_df |>
filter(factor == "healthy_life_expectancy")|>
plot_ly(
x = ~year, y = ~average_score, color = ~region,
mode = 'lines+markers', type = 'scatter', colors = 'viridis',
name = ~region) |>
layout(
title = paste("Regional Trend for Life Expectancy from 2015 to 2019"),
xaxis = list(title = "Year"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
hap_df |>
mutate(region = fct_reorder(region, freedom_to_make_life_choices))|>
group_by(region, year)|>
summarise(
average_freedom = mean(freedom_to_make_life_choices, na.rm = TRUE)
) |>
plot_ly (
x = ~region, y = ~average_freedom, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "Freedom to Make Choice By Region",
xaxis = list(title = "Region"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
Regional Trend
regional_hap_df |>
filter(factor == "freedom_to_make_life_choices")|>
plot_ly(
x = ~year, y = ~average_score, color = ~region,
mode = 'lines+markers', type = 'scatter', colors = 'viridis',
name = ~region) |>
layout(
title = paste("Regional Trend for Freedom to Make Life Choices from 2015 to 2019"),
xaxis = list(title = "Year"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
hap_df |>
mutate(region = fct_reorder(region, perception_of_corruption))|>
group_by(region, year)|>
summarise(
average_corruption = mean(perception_of_corruption, na.rm = TRUE)
) |>
plot_ly (
x = ~region, y = ~average_corruption, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "Perception of Corruption By Region",
xaxis = list(title = "Region"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `region = fct_reorder(region, perception_of_corruption)`.
## Caused by warning:
## ! `fct_reorder()` removing 1 missing value.
## ℹ Use `.na_rm = TRUE` to silence this message.
## ℹ Use `.na_rm = FALSE` to preserve NAs.
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
Regional Trend
regional_hap_df |>
filter(factor == "perception_of_corruption")|>
plot_ly(
x = ~year, y = ~average_score, color = ~region,
mode = 'lines+markers', type = 'scatter', colors = 'viridis',
name = ~region) |>
layout(
title = paste("Regional Trend for Perception of Corruption from 2015 to 2019"),
xaxis = list(title = "Year"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
hap_df |>
mutate(region = fct_reorder(region, generosity))|>
group_by(region, year)|>
summarise(
average_generosity = mean(generosity, na.rm = TRUE)
) |>
plot_ly (
x = ~region, y = ~average_generosity, color = ~year,
type = "bar", colors = "viridis") |>
layout(
title = "Generosity By Region",
xaxis = list(title = "Region"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
Regional Trend
regional_hap_df |>
filter(factor == "generosity")|>
plot_ly(
x = ~year, y = ~average_score, color = ~region,
mode = 'lines+markers', type = 'scatter', colors = 'viridis',
name = ~region) |>
layout(
title = paste("Regional Trend for Generosity from 2015 to 2019"),
xaxis = list(title = "Year"),
yaxis = list(title = "Average Score"),
autosize = F, width = 1000, height = 500
)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
library(ggplot2)
library(stringr)
library(maps)
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(magrittr)
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
##
## set_names
## The following object is masked from 'package:tidyr':
##
## extract
library(rvest)
##
## Attaching package: 'rvest'
## The following object is masked from 'package:readr':
##
## guess_encoding
library(countrycode)
iso_codes = countrycode::codelist[, c("un.name.en", "iso3c")]
names(iso_codes) = c("Country", "ISO3")
world_data <- ggplot2::map_data('world')
world_data <- fortify(world_data)
hap_df['ISO3'] <- iso_codes$ISO3[match(hap_df$country, iso_codes$Country)]
world_data["ISO3"] <- iso_codes$ISO3[match(world_data$region, iso_codes$Country)]
hap_df = hap_df |>
mutate(
ISO3 = if_else(country == "United States", "USA", ISO3),
ISO3 = if_else(country == "United Kingdom", "GBR", ISO3),
ISO3 = if_else(country == "Venezuela", "VEN", ISO3),
ISO3 = if_else(country == "Czech Republic", "CZE", ISO3),
ISO3 = if_else(country == "Taiwan", "TWN", ISO3),
ISO3 = if_else(country == "South Korea", "KOR", ISO3),
ISO3 = if_else(country == "Bolivia", "BOL", ISO3),
ISO3 = if_else(country == "Moldova", "MDA", ISO3),
ISO3 = if_else(country == "Russia", "RUS", ISO3),
ISO3 = if_else(country == "Hong Kong", "CHN", ISO3),
ISO3 = if_else(country == "Vietnam", "VNM", ISO3),
ISO3 = if_else(country == "Turkey", "TUR", ISO3),
ISO3 = if_else(country == "Macedonia", "MKD", ISO3),
ISO3 = if_else(country == "Laos", "LAO", ISO3),
ISO3 = if_else(country == "Swaziland", "SWZ", ISO3),
ISO3 = if_else(country == "Palestinian Territories", "PSE", ISO3),
ISO3 = if_else(country == "Iran", "IRN", ISO3),
ISO3 = if_else(country == "Congo (Kinshasa)", "COD", ISO3),
ISO3 = if_else(country == "Congo (Brazzaville)", "COG", ISO3),
ISO3 = if_else(country == "Tanzania", "TZA", ISO3),
ISO3 = if_else(country == "Syria", "SYR", ISO3),
ISO3 = if_else(country == "Puerto Rico", "PRI", ISO3)
)|>
mutate(country = recode(str_trim(country),
"United States" = "USA",
"United Kingdom" = "UK",
"Congo (Kinshasa)" = "Democratic Republic of the Congo",
"Congo (Brazzaville)" = "Republic of Congo"))
world_data = world_data|>
mutate(country = region)|>
select(-region)
worldjoin <- inner_join(world_data, hap_df, by = "country")
## Warning in inner_join(world_data, hap_df, by = "country"): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 11 of `x` matches multiple rows in `y`.
## ℹ Row 153 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
## "many-to-many"` to silence this warning.
## compile all map theme configurations
cleanup <- theme(
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank(),
panel.background = element_rect(fill = "white"),
plot.title = element_text(hjust = 0.5)
)
## plot our merged data
gdp <- worldjoin %>%
ggplot(mapping = aes(
x=long,
y=lat,
group=group)) +
scale_fill_viridis_c(option = "C") +
coord_fixed(1.3) +
geom_polygon(aes(fill=gdp_per_capita)) +
ggtitle("GDP Per Capita") +
cleanup
gdp
Social Support